<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/25 0025
 * Time: 上午 1:15
 */

namespace app\common\lib\orm;


trait ModelColumns
{

    private static function createTableSql($table, $columns)
    {
        $column_sql = "`id` int(11) NOT NULL AUTO_INCREMENT,";
        $instance = self::instance();
        foreach ($columns as $item) {
            if(
                isset($item["type"]) &&
                isset($item["column"])
            ){
                $type = $item["type"];
                $column = $item["column"];
            }else{
                continue;
            }
            if(method_exists($instance,$type)){
                $column_sql .= call_user_func_array(
                    [$instance,$type],[
                        $column,
                        isset($item["memo"])?$item["memo"]:$item["memo"],
                        isset($item["length"])?$item["length"]:$item["length"],
                    ]
                );
            }
        }
        $column_sql .= "  `created_time` timestamp(0) NOT NULL  DEFAULT '0-0-0 0:0:0' ,";
        $column_sql .= "`updated_time` timestamp(0) NOT NULL  DEFAULT '0-0-0 0:0:0' ON UPDATE CURRENT_TIMESTAMP(0),";
        $column_sql .= " PRIMARY KEY (`id`) ";
        $sql = sprintf("  CREATE TABLE `%s`(%s)", $table, $column_sql);
        return $sql;
    }
    private static function text($column,$memo = "",$length = 0)
    {
        return "`$column` text NULL COMMENT '" . $memo . "',";
    }

    private static function varchar($column, $memo = "", $length = 127)
    {
        return "`$column` varchar($length) NOT NULL DEFAULT '' COMMENT '" . $memo . "',";
    }

    private static function tinyint($column, $memo = "",$length = 4)
    {
        return "`$column` tinyint(4) NOT NULL DEFAULT 0 COMMENT '" . $memo . "',";
    }

    private static function int($column, $memo = "", $length = 11)
    {
        return "`$column` int($length) NOT NULL DEFAULT 0 COMMENT '" . $memo . "',";
    }

    private static function decimal($column, $memo = "",$length = 0)
    {
        return "`$column` decimal(8, 2) NOT NULL DEFAULT 0 COMMENT '" . $memo . "',";
    }
    private static function timestamp($column, $memo = "",$length = 0)
    {
        return "`$column` timestamp(0) NOT NULL DEFAULT '0-0-0 0:0:0' COMMENT '" . $memo . "',";
    }
}